package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.SpuManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
@Api(tags ="商品属性SPU管理")
public class SpuManageController {

    @Autowired
    private SpuManageService spuManageService;


    /**
     * /admin/product/cancelSale/{skuId}
     * 下架
     * Get
     */
    @GetMapping("/cancelSale/{skuId}")
    @ApiOperation("下架")
    public Result cancelSale(@PathVariable Long skuId){
        spuManageService.cancelSale(skuId);
        return Result.ok();
    }
    /**
     * /admin/product/onSale/{skuId}
     * 上架
     * Get
     */
    @GetMapping("/onSale/{skuId}")
    @ApiOperation("sku上架")
    public Result onSale(@PathVariable Long skuId){
        spuManageService.onSale(skuId);
        return Result.ok();
    }


    /**
     * /admin/product/list/{page}/{limit}
     * sku分页列表
     * Get
     */
    @GetMapping("/list/{page}/{limit}")
    @ApiOperation("sku分页列表")
    public Result SkuList(
            @PathVariable Long page,
            @PathVariable Long limit
    ){
        IPage<SkuInfo> skuInfoPage =spuManageService.SkuList(page, limit);
        return Result.ok(skuInfoPage);
    }


    /**
     * /admin/product/saveSkuInfo
     *保存SkuInfo
     * Post
     */
    @PostMapping("/saveSkuInfo")
    @ApiOperation("保存SkuInfo")
    public Result saveSkuInfo(
            @RequestBody SkuInfo skuInfo
    ){
        spuManageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }



    /**
     * /admin/product/{page}/{limit}
     * spu分页列表
     * Get
     */
    @GetMapping("/{page}/{limit}")
    @ApiOperation("spu分页列表")
    public Result getSpuList(
            @PathVariable Long page,
            @PathVariable Long limit,
            SpuInfo spuInfo
    ){
        IPage<SpuInfo> spuInfoPage = new Page<>(page,limit);
        IPage<SpuInfo> spuInfoPageList=spuManageService.selectSpuInfoList(spuInfoPage,spuInfo.getCategory3Id());
        return Result.ok(spuInfoPageList);
    }

    /**
     * http://localhost/admin/product/baseSaleAttrList
     *获取销售属性数据
     * Get
     */
    @GetMapping("/baseSaleAttrList")
    @ApiOperation("获取销售属性数据")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> saleAttrList=spuManageService.baseSaleAttrList();
        return Result.ok(saleAttrList);
    }

    /**
     * /admin/product/saveSpuInfo
     * 保存Spu
     * Post
     */
    @PostMapping("/saveSpuInfo")
    @ApiOperation("保存Spu")
    public Result saveSpuInfo(
            @RequestBody SpuInfo spuInfo
    ){
        spuManageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    /**
     * /admin/product/spuSaleAttrList/{spuId}
     * 根据spuId 查询销售属性
     * Get
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
    @ApiOperation("根据spuId查询销售属性")
    public Result spuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttr> spuSaleAttrList=spuManageService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    /**
     * /admin/product/spuImageList/{spuId}
     * 根据spuId 获取spuImage 集合
     * Get
     */
    @GetMapping("/spuImageList/{spuId}")
    @ApiOperation("根据spuId 获取spuImage 集合")
    public Result spuImageList(@PathVariable Long spuId){
        List<SpuImage> spuImageList=spuManageService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }



}
